// pages/productDetails/material/material.js
var http = require('../../../utils/server.js')
var util = require('../../../utils/util.js')
Page({
  data: {
    topActive: 0,
    topMenuData: [{
      title: "图片",
      img: "/images/deta_ico_img_nor.png",
      activeImg: "/images/deta_ico_img_sel.png"
    }, {
      title: "段子",
      img: "/images/deta_ico_par_nor.png",
      activeImg: "/images/deta_ico_par_sel.png"
    }],
    imgs: [{
      src: '/images/ww.png',
      cover: false
    }],
    selectAll: false,
    selectArr: [],
    passage: [],
    width: [],
    height: []
  },

  onLoad: function(option) {
    var that = this
    that.setData({
      product_id: option.product_id
    })

    http.post({
      url: "product/getPosters",
      product_id: that.data.product_id
    }, function(data) {
      console.log('getPosters', data)
      var imgs = [],
        selectArr = [],
        passage = [];
      for (i in data['imgs']) {
        var img = {
          src: data['imgs'][i],
          cover: false
        }
        imgs.push(img)
        selectArr.push(false)
      }
      passage = data['product_passage']
      that.setData({
        imgs: imgs,
        selectArr: selectArr,
        passage: passage
      })
    })
  },

  topMenuTap(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      topActive: index
    })
  },
  selectOneTap(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var selectArr = that.data.selectArr
    selectArr[index] = !selectArr[index]
    var allFlag = 0
    var selectAll = that.data.selectAll
    for (i in selectArr) {
      if (selectArr[i])
        allFlag++
    }
    selectAll = allFlag == selectArr.length ? true : false
    this.setData({
      selectArr: selectArr,
      selectAll: selectAll
    })
  },
  selectAllTap() {
    var that = this
    var selectAll = that.data.selectAll
    var selectArr = that.data.selectArr
    selectAll = !that.data.selectAll
    if (!selectAll) {
      for (i in selectArr) {
        selectArr[i] = false
      }
    }
    this.setData({
      selectArr: selectArr,
      selectAll: selectAll
    })
  },
  preview(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var urls = []
    var imgs = that.data.imgs
    for (i in imgs) {
      urls.push(imgs[i].src)
    }
    wx.previewImage({
      current: imgs[index].src,
      urls: urls
    })
  },
  deletePassage(e) {
    var passage_id = e.currentTarget.dataset.passage_id
    console.log(e.currentTarget.dataset)
    var that = this
    http.post({
      url: "product/deletePassage",
      product_id: that.data.product_id,
      passage_id: passage_id
    }, function(data) {
      var passage = that.data.passage
      for (i in passage) {
        if (passage[i].id == passage_id)
          passage.splice(i, 1)
      }
      that.setData({
        passage: passage
      })
      wx.showToast({
        title: '删除成功',
        icon: 'success'
      })
    })
  },
  save: function() {
    var that = this;
    var selectArr = that.data.selectArr
    var saveArr = []
    for (i in selectArr) {
      if (selectArr[i])
        saveArr.push(that.data.imgs[i].src)
    }
    for (i in saveArr) {
      wx.getImageInfo({
        src: saveArr[i],
        success: function(res) {
          console.log(res.path)
          wx.saveImageToPhotosAlbum({
            filePath: res.path,
            success(res) {
              console.info(res)
            },
            fail(res) {
              console.info(res)
            }
          })
        }
      })
    }
  },
  imgHeight: function(e) {
    var index = e.currentTarget.dataset.index
    var imgw = e.detail.width;
    var imgh = e.detail.height;
    var width = this.data.width
    var height = this.data.height
    var s = imgw / imgh
    width[index] = 164 * s > 218 ? 218 : 164 * s
    height[index] = 218 / s > 164 ? 164 : 218 / s
    this.setData({
      width: width,
      height: height
    })
  },
  copy: function(e) {
    var data = e.currentTarget.dataset.content
    wx.setClipboardData({
      data: data,
      success: function(res) {
        util.showSuccess('复制成功')
      }
    })
  }

})